package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.common.enums.SaveMethodEnum;
import vip.specimen.system.common.implement.AliOssUpload;
import vip.specimen.system.common.implement.QiNiuUpload;
import vip.specimen.system.common.implement.TencentUpload;
import vip.specimen.system.datasource.entity.SystemResourceEntity;
import vip.specimen.system.datasource.entity.SystemResourceSuffixEntity;
import vip.specimen.system.repository.SystemResourceRepository;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/3/11 14:19
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemResourceService {
    @Resource
    private SystemResourceRepository resourceRepository;
    @Resource
    private SystemSaveConfigService saveConfigService;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增文件
     *
     * @param resourceEntity 文件
     * @return 文件
     */
    @Transactional
    public SystemResourceEntity insert(SystemResourceEntity resourceEntity) {
        return this.resourceRepository.save(resourceEntity);
    }

    /**
     * 根据资源key删除
     *
     * @param key key
     * @return 成功 or 失败
     */
    @Transactional
    public boolean delete(String key) {
        SystemResourceEntity resourceEntity = this.resourceRepository.findSystemResourceEntityByFileKeyOrderByCreateTimeDesc(key);
        if (null != resourceEntity) {
            String methodName = this.saveConfigService.getConfig().getSaveMethod().name();
            if (methodName.equals(SaveMethodEnum.QiNiu.name())) { // 七牛云
                new QiNiuUpload().delete(resourceEntity.getFileKey());
            }
            this.resourceRepository.delete(resourceEntity);
            return true;
        }
        return false;

    }

    /**
     * 根据后缀查询资源
     *
     * @param suffixEntities 后缀集
     * @return 资源集
     */
    public Page<SystemResourceEntity> findResource(List<SystemResourceSuffixEntity> suffixEntities) {
        JSONObject param = CommonUtil.pageParams(this.request);
        Pageable pageable = DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime"));
        return this.resourceRepository.findAllBySuffixEntityInOrderByCreateTimeDesc(suffixEntities, pageable);
    }

    /**
     * 根据uuid删除资源
     *
     * @param uuid uuid
     * @return 成功 or 失败
     */
    @Transactional
    public boolean delete(UUID uuid) {
        if (this.resourceRepository.existsById(uuid)) {
            SystemResourceEntity resource = this.resourceRepository.findById(uuid).get();
            if (resource.getSaveMethodEnum().name().equals(SaveMethodEnum.QiNiu.name())) { // 七牛云
                new QiNiuUpload().delete(resource.getFileKey());
            } else if (resource.getSaveMethodEnum().name().equals(SaveMethodEnum.ALiYun.name())) {
                new AliOssUpload().delete(resource.getFileKey());
            } else if (resource.getSaveMethodEnum().name().equals(SaveMethodEnum.Tencent.name())) {
                new TencentUpload().delete(resource.getFileKey());
            }
            this.resourceRepository.delete(resource);
            return true;
        }
        return false;
    }


    /**
     * 根据资源uuid 批量删除
     *
     * @param uuids UUID数组
     * @return 成功数
     */
    @Transactional
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    /**
     * 统计数目
     *
     * @return 数目
     */
    public long count() {
        return this.resourceRepository.count();
    }
}
